package com.link2room.rodimus.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.rodimus.dto.param.api.MemberLevelBaseFindParamDto;
import com.link2room.rodimus.dto.param.base.MemberLevelBaseDto;
import com.link2room.rodimus.dto.param.base.MemberLevelDetailDto;
import com.link2room.rodimus.entity.MemberLevelBaseEntity;
import com.link2room.rodimus.entity.MemberLevelDetailEntity;
import com.link2room.rodimus.facade.IMemberLevelFacadeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by LN on 2017/11/15
 */
@Component("memberLevelFacadeService")
public class MemberLevelFacadeServiceImpl implements IMemberLevelFacadeService {
    @Resource
    private ICommonService commonService;

    @Override
    public QueryResult<MemberLevelBaseDto> selectMemberLevelBaseDto(MemberLevelBaseFindParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is null.");
        String memberLevel = paramDto.getMemberLevel();
//        L2RAssert.stringBlank(memberLevel, "[memberLevel] is null.");
        String needDetail = paramDto.getNeedDetail();
        MemberLevelBaseEntity find = new MemberLevelBaseEntity();
        find.setMemberLevel(memberLevel);
        find.setLhotelGroupCode(lhotelGroupCode);
        QueryResult<MemberLevelBaseEntity> qers = commonService.findAllEQ(MemberLevelBaseEntity.class, find, paramDto.getCurrentPage(), paramDto.getPageSize());
        QueryResult<MemberLevelBaseDto> res = ClassConverUtil.copyQueryResultToDtoQueryResult(qers, MemberLevelBaseDto.class, true);
        if (res.getTotalrecord() == 0|| CollectionUtils.isEmpty(res.getResultlist())) {
            return res;
        } else {

            QueryResult<MemberLevelDetailEntity> detailQres = null;
            if(DefaultValue.T.equals(needDetail)){
                List<String> memberLevels = res.getResultlist().stream().map(MemberLevelBaseDto::getMemberLevel).collect(Collectors.toList());
                L2RCondition l2RCondition = new L2RCondition();
                l2RCondition.setAndConditions(
                        new ConditionDetail[]{
                                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,lhotelGroupCode),
                                new ConditionDetail(MemberLevelDetailEntity.MemberLevelDetailEntity_.memberLevel.toString(), CriteriaUtil.Operator.IN,memberLevels),
                        }
                );
                detailQres = commonService.findAllByCondition(MemberLevelDetailEntity.class, l2RCondition, null, null);
            }
            if(detailQres == null || CollectionUtils.isEmpty(detailQres.getResultlist())){
                return res;
            }else {
                QueryResult<MemberLevelDetailEntity> finalDetailQres = detailQres;
                res.getResultlist().forEach(baseDto->{
                    baseDto.setMemberLevelDetailDtos(
                            finalDetailQres.getResultlist().stream()
                                    .filter(detailEntity->detailEntity.getMemberLevel().equals(baseDto.getMemberLevel()))
                                    .map(detailEntity->ClassConverUtil.copyProperties(detailEntity,MemberLevelDetailDto.class))
                                    .collect(Collectors.toList())
                    );
                });
                return res;
            }
        }
    }
}
